//
//  COEdditNameController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/17.
//

import UIKit

class COEdditNameController: COBaseController,UITextFieldDelegate {
    
    var nameEditBlock:((_ name : String)->Void)?
    
    lazy var containView: UIView = {
        let con = UIView()
        con.backgroundColor = .white
        return con
    }()
    
    // hi
    lazy var nameTxt: UITextField = {
        let hi = UITextField()
        hi.textColor = UIColor.colorWithHex("#7863A6")
        hi.backgroundColor = UIColor.colorWithHex("#F5F5F5", 1)
        hi.borderStyle = .roundedRect
        hi.font = UIFont.pingfangFont(ofSize:PTW(18), weight: .medium)
        let attributes = [NSAttributedString.Key.font:UIFont.pingfangFont(ofSize: PTW(15), weight: .medium),
                          NSAttributedString.Key.foregroundColor:UIColor.colorWithHex("#7863A6", 0.5)]
        hi.attributedPlaceholder = NSAttributedString.init(string: "input your name (15 limit)", attributes: attributes)
        hi.modifyClearButton(KImaged("coco_editTxt_close"))
        hi.clearButtonMode = .whileEditing  //编辑时出现清除按钮
        let leftView = UIView(frame:CGRect(x: 0, y: 0, width: PTW(10), height: PTW(40)))
        hi.leftViewMode = .always
        hi.leftView = leftView
        hi.delegate = self
        return hi
    }()
    
    // 按钮
    lazy var doneBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(16), weight: .bold)
        button.setTitleColor(.white, for: .normal)
        button.setTitle("Done", for: .normal)
        button.addTap { [weak self] ges in
            guard let `self` = self else { return }
            self.changeUserName()
        }
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.addnav()
        self.title = "Nickname"
        self.view.addSubview(self.containView)
        self.containView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.navView.snp.bottom)
        }
        self.containView.addSubview(nameTxt)
        self.containView.addSubview(doneBtn)
        nameTxt.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(PTW(60))
            make.height.equalTo(PTW(50))
            make.leading.equalToSuperview().offset(PTW(15))
            make.trailing.equalToSuperview().offset(-PTW(15))
        }
        doneBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(PTW(225), PTW(65)))
            make.top.equalTo(nameTxt.snp.bottom).offset(PTW(200))
            make.centerX.equalToSuperview()
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.doneBtn.clipsAllCorners(radius: PTW(65/2))
        self.doneBtn.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range: PTW(225))
        self.containView.clipsCorners(corners: [.topLeft,.topRight], radius: PTW(30))
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let text = textField.text!
        let len = text.count + string.count - range.length
        return len<=15
    }
    
    // 更改姓名
    func changeUserName(){
        self.nameTxt.endEditing(true)
        if self.nameTxt.text?.count == 0{
            UIToolHelper.makeToast("Please input nickname")
            return
        }
        if self.nameEditBlock != nil {
            self.nameEditBlock!(self.nameTxt.text!)
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {
            self.navigationController?.popViewController(animated: true)
        })
    }

    
}
